// live/admin/apply.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    cat_name:'',
    select_cat: !1,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (e) {
    getApp().page.onLoad(this, e);
    this.getInfo();
    this.getLiceConfig();
  },
  /**
   * 获取申请内容
   */
  getInfo: function () {
    getApp().core.showLoading({
      title: "加载中"
    })
    var that = this;
    getApp().request({
      url: getApp().api.live.get_info,
      success: function (t) {
        if (t.code == 0) {
          if (t.data.live_info) {
            t.data.live_cat.forEach(function (item) {
              if (item.id == t.data.live_info.cat_id) {
                that.setData({
                  cat_name: item.name,
                  cat_id: item.id
                })
              }
            });
          } else {
            that.setData({
              cat_name: t.data.live_cat[0].name,
              cat_id: t.data.live_cat[0].id
            })
          }
          that.setData(t.data)
        }
      },
      complete: function (t) {
        getApp().core.hideLoading();
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    getApp().page.onShow(this);
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  formSubmit: function (e) {
    var data = e.detail.value;
    if (!data || !data.desc || !data.mobile || !data.nickname || !data.name || !this.data.cat_id) {
      wx.showModal({
        title: '提示',
        content: '请补充完整内容',
        showCancel: false
      });
      return false;
    }else {
      getApp().core.showLoading({
        title: "提交中"
      })
      var that = this;
      getApp().request({
        url: getApp().api.live.submit_apply,
        method: "post",
        data: {
          name: data.name,
          desc: data.desc,
          mobile: data.mobile,
          nickname: data.nickname,
          cat_id: that.data.cat_id
        },
        success: function (t) {
          if (t.code == 0) {
            wx.showModal({
              title: '提示',
              content: '提交成功',
              showCancel: !1,
              success: function (res) {
                if (res.confirm) {
                  getApp().core.switchTab({
                    url: "/pages/user/user"
                  });
                } else if (res.cancel) {
                  console.log('用户点击取消')
                }
              }
            })
          }else {
            wx.showModal({
              title: '提示',
              content: t.msg,
              showCancel: false
            });
          }
        },
        complete: function (t) {
          getApp().core.hideLoading();
        }
      })
    }
  },
  bindChange: function (e) {
    const val = e.detail.value
    console.log(val[0]);
    var cat_name = this.data.live_cat[val[0]].name
    var cat_id = this.data.live_cat[val[0]].id
    this.setData({
      cat_name: cat_name,
      cat_id: cat_id
    })
  },
  select_cat_f: function () {
    var that = this;
    that.setData({
      select_cat: !that.data.select_cat
    })
  },
  // 获取直播配置
  getLiceConfig: function () {
    var that = this;
    getApp().request({
      url: getApp().api.live.get_config,
      method: "post",
      success: function (t) {
        if (t.code == 0) {
          that.setData({
            user_info: t.data.user_info,
            config: t.data.config
          })
        }
      },
      complete: function (t) {
        getApp().core.hideLoading();
      }
    })
  }
})